package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //需要新增清楚缓存的功能
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        DishPageDTO result = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 删除菜品
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids) {
        log.info("删除菜品:{}", ids);
        dishService.deleteById(ids);

        //因为这是批量删除所以 我们需要判断是来自于哪个菜品的换从 太麻烦了所以选择全部干掉
        deleteCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishDTO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品:{}", id);
        DishDTO dishDTO = dishService.getById(id);
        return Result.success(dishDTO);
    }


    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //修改的话也是同理我们也需要判断他的类型是套餐还是菜品所有我们也选择全部删掉
        deleteCache("dish_*");
        return Result.success();
    }

    /**
     * 根据套餐Id查询需要的菜品
     */
    @ApiOperation("根据套餐Id查询需要的菜品")
    @GetMapping("/list")
    public Result list(@RequestParam Integer categoryId) {
        log.info("根据套餐Id查询需要的菜品:{}", categoryId);
        List<DishDTO> dishDTOList = dishService.list(categoryId);
        return Result.success(dishDTOList);
    }

    /**
     * 菜品起售停售
     * @param pattern
     */
    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        log.info("菜品起售停售:{},{}", status, id);
        dishService.startOrStop(status, id);

        //起售停售的话 我们只需要删除菜品的缓存即可
        deleteCache("dish_*");
        return Result.success();
    }

    //抽取出来一个删除所有缓存的方法
    private void deleteCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
